<#assign pojo=oneToManyPojo.getOne()>
<#assign many=oneToManyPojo.getMany()>
<#assign ones=oneToManyPojo.getOnes()>
<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${pojo.getRealPackageAndClassPath()}">
<#assign masterid = pojo.getDeclarationNameFirstLetterLower()+"Id">
<#assign module = oneToManyPojo.getModule()>
<#assign system = oneToManyPojo.getSystem()>
'use strict';

app.controller('${declarationName}Controller', function ($rootScope, $scope, $resource, $uibModal, $log, $state, $stateParams, $location, SweetAlert, ${declarationName}Service, AngularUtils) {

    $scope.query = {};

    var columnDefs = [
        // this row just shows the row index, doesn't use any data from the row
        {
            headerName: "#", width: 50, cellRenderer: function (params) {
            return params.node.id + 1;
        }
        },
<#foreach field in fields>
        {headerName: "<#if field.getComment()?exists>${field.getComment()}<#else>${field.getName()}</#if>", field: "${field.getName()}"},
</#foreach>
        {
            headerName: '操作',
            pinned: 'right',
            template: ' <a class="btn btn-primary btn-xs" ng-click="add${declarationName}()"><i class="glyphicon glyphicon-plus"></i> 新增</a>' +
            ' <a class="btn btn-azure btn-xs" ng-click="modify${declarationName}(data)"><i class="fa fa-wrench"></i> 修改</a>' +
            ' <button class="btn btn-red btn-xs" ng-click="remove${declarationName}(data)"><i class="fa fa-trash-o"></i> 删除</button>'
        }
    ];
    var get${declarationName}DataSource=function  () {
        var dataSource = {
            // rowCount:1,
            getRows: function (params) {
                var pageSize=$scope.gridOptions${declarationName}.paginationPageSize;
                var pageIndex=Math.round(params.startRow/pageSize)+1;
                // console.log(pageIndex+" "+pageSize);
                $scope.query.pageIndex=pageIndex;
                $scope.query.pageSize= pageSize;
                ${declarationName}Service.find${declarationName}ByPage($scope.query,function (result) {
                    // console.log(result.data.result);
                    params.successCallback(result.data.result, result.data.total);
                },function (result) {

                });
            }
        };
        return dataSource;
    }
    $scope.gridOptions${declarationName} = {
        // note - we do not set 'virtualPaging' here, so the grid knows we are doing standard paging
        enableSorting: true,
        enableFilter: true,
        rowHeight: 30,
        angularCompileRows: true,
        // debug: true,
        rowSelection: 'multiple',
        enableColResize: true,
        paginationPageSize: 500,
        columnDefs: columnDefs,
        rowModelType: 'pagination',
        datasource: get${declarationName}DataSource(500)
    };
    $scope.search${declarationName} = function () {
        var dataSource = get${declarationName}DataSource($scope.gridOptions${declarationName}.paginationPageSize);
        $scope.gridOptions${declarationName}.api.setDatasource(dataSource);
    }
    $scope.showDetail = false;
    $scope.current${declarationName} = {};
    $scope.add${declarationName} = function () {
        $location.url('${module}/${declarationNameFirstLetterLower}Detail');
    };
    $scope.modify${declarationName} = function (data) {
        $location.url('${module}/${declarationNameFirstLetterLower}Detail?${declarationNameFirstLetterLower}Id=' + data.id);
    };
    $scope.remove${declarationName} = function (data) {
        ${declarationName}Service.delete${declarationName}(data,function (result) {
            $scope.search${declarationName}();
        });
    };
});